import 'package:eplorex/dao/notification_msg_dao.dart';
import 'package:eplorex/pages/message/notification/NotificationCell.dart';
import 'package:flutter/material.dart';

import '../../../models/notification_msg_model.dart';

class LikeNotificationPage extends StatefulWidget {
  const LikeNotificationPage({super.key});

  @override
  State<LikeNotificationPage> createState() => _LikeNotificationPageState();
}

class _LikeNotificationPageState extends State<LikeNotificationPage> {
  Future<List<NotificationMsgModel>?>? _notificaitonMsg;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("收到的消息"),
      ),
      body: FutureBuilder(
        future: _notificaitonMsg,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {

          if(snapshot.connectionState==ConnectionState.waiting || !snapshot.hasData){
            return Text("loading....");
          }
          List<NotificationMsgModel>? notificationMsgs= snapshot.data;
          return ListView.builder(itemBuilder: (BuildContext context, int index)=>itemBuilder(context,index,notificationMsgs![index]),itemCount: notificationMsgs!.length,);
        },
      ),
    );
  }

  Widget? itemBuilder(BuildContext context, int index,NotificationMsgModel notificationMsg) {

    return NotificationCell(userName: notificationMsg.userName!
      ,date: notificationMsg.date, worksUrl: notificationMsg.worksUrl,
      avatar:notificationMsg.avatar ,infoMsg: notificationMsg.infoMsg,);
  }

  @override
  void initState() {
    _notificaitonMsg = NotificationMsgDao.queryUserNotification();
  }
}
